local fk__jingzhu = fk.CreateSkill {
  name = "fk__jingzhu",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["fk__jingzhu"] = "经注",
  [":fk__jingzhu"] = "锁定技，每当实体牌对你结算结束后，你须选择一项：1.将此牌置于你武将牌上，称为“经注”；2.观看一名其他角色手牌，然后你可以移去一张“经注”并使用其手牌中一张与移去的“经注”相同类型、不同牌名的牌。",

  ["#fk__jingzhu_put"] = "将%arg置入“经注”",
  ["#fk__jingzhu_watch"] = "观看一名角色手牌",
  ["#fk__jingzhu-choose"] = "经注：观看一名其他角色手牌",
  ["#fk__jingzhu-use"] = "经注：请使用 %arg",
  ["#fk__jingzhu-card"] = "经注：移去一张“经注”并选择一张与之同类异名的牌",

  ["$fk__jingzhu1"] = "夫子没而微言绝，后学当治经明义。",
  ["$fk__jingzhu2"] = "秦火重而群经毁，我辈须彰灼圣训。",
}

fk__jingzhu:addEffect(fk.CardUseFinished, {
  anim_type = "control",
  derived_piles = "fk__jingzhu",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(fk__jingzhu.name) and data.tos and table.contains(data.tos, player)
    and not data.card:isVirtual() and data.card.name == Fk:getCardById(data.card.id, true).name
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local all_choices = {"#fk__jingzhu_put:::"..data.card.name,"#fk__jingzhu_watch"}
    local choices = {}
    if room:getCardArea(data.card) == Card.Processing or table.contains(player:getCardIds("e"), data.card:getEffectiveId()) then
      table.insert(choices, all_choices[1])
    end
    local targets = table.filter(room.alive_players, function (p)
      return player ~= p and not p:isKongcheng()
    end)
    if #targets > 0 and #player:getPile(fk__jingzhu.name) > 0 then
      table.insert(choices, all_choices[2])
    end
    if #choices == 0 then return end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = fk__jingzhu.name,
      all_choices = all_choices,
    })
    if choice == all_choices[1] then
      player:addToPile(fk__jingzhu.name, data.card, true, fk__jingzhu.name)
    else
      local tos = room:askToChoosePlayers(player, {
        targets = targets,
        min_num = 1,
        max_num = 1,
        skill_name = fk__jingzhu.name,
        prompt = "#fk__jingzhu-choose",
        cancelable = false,
      })
      local to = tos[1]
      local result = room:askToPoxi(player, {
        poxi_type = "fk__jingzhu",
        data = {
          { fk__jingzhu.name, player:getPile(fk__jingzhu.name) },
          { to.general, to:getCardIds("h") },
        },
        cancelable = true,
      })
      if #result == 2 then
        room:moveCards({
          ids = {result[1]},
          from = player,
          toArea = Card.DiscardPile,
          moveReason = fk.ReasonPutIntoDiscardPile,
          skillName = fk__jingzhu.name,
          proposer = player,
        })
        local cid = result[2]
        if not player.dead and table.contains(to:getCardIds("h"), cid) then
          room:askToUseRealCard(player, {
            pattern = {cid},
            skill_name = fk__jingzhu.name,
            prompt = "#fk__jingzhu-use:::"..Fk:getCardById(cid):toLogString(),
            skip = false,
            cancelable = false,
            extra_data = {expand_pile = {cid}}
          })
        end
      end
    end
  end,
})

Fk:addPoxiMethod{
  name = "fk__jingzhu",
  card_filter = function(to_select, selected, data)
    if data == nil then return false end
    if #selected == 0 then
      return table.contains(data[1][2], to_select)
    elseif #selected == 1 then
      if not table.contains(data[1][2], selected[1]) then return false end
      local card = Fk:getCardById(to_select)
      local first = Fk:getCardById(selected[1])
      return Self:canUse(card, {bypass_times = true}) and card.trueName ~= first.trueName and card.type == first.type
    end
  end,
  feasible = function(selected, data, extra_data)
    if data == nil or #selected ~= 2 then return false end
    return #table.filter(selected, function (id)
      return table.contains(data[1][2], id)
    end) == 1
  end,
  prompt = "#fk__jingzhu-card",
  default_choice = function ()
    return {}
  end,
}

return fk__jingzhu
